<template>
  <cp-nav-bar title="登录" rightText="注册" @click-right="clickRight"></cp-nav-bar>
  <div class="login">
    <div class="login-head">
      <h3>{{ isPass ? '密码登录' : '短信验证码登录' }}</h3>
      <a href="javascript:;" @click="isPass = !isPass">
        <span>{{ !isPass ? '密码登录' : '短信验证码登录' }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <van-form @submit="login" autocapitalize="off">
      <van-field
        name="mobile"
        v-model="mobile"
        :rules="mobileRules"
        placeholder="请输入手机号码"
        type="tel"
      >
      </van-field>
      <van-field
        v-if="isPass"
        v-model="password"
        :rules="passwordRules"
        placeholder="请输入密码"
        :type="isShowPass ? 'text' : 'passowrd'"
      >
        <template #button>
          <cp-icon
            @click="isShowPass = !isShowPass"
            :name="`login-eye-${isShowPass ? 'on' : 'off'}`"
          ></cp-icon>
        </template>
      </van-field>

      <van-field v-model="code" :rule="codeRuules" v-else placeholder="短信验证码">
        <template #button>
          <span @click="send" :class="{ active: time > 0 }">
            {{ time > 0 ? `${time}s后再发送` : '发送验证码' }}
          </span>
        </template>
      </van-field>

      <div class="cp-cell">
        <van-checkbox v-model="agree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐藏条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button native-type="submit" block round type="primary"> 登录 </van-button>
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码?</a>
      </div>
    </van-form>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { mobileRules, passwordRules } from '@/utils/rules'
import { loginByPassword, loginByMobile } from '@/api/user'
import { useUserStore } from '@/stores'
import { useRouter, useRoute } from 'vue-router'
import { showSuccessToast, showFailToast } from 'vant'
import 'vant/es/toast/style'
import { useSendCode } from './service/index'
const clickRight = () => {
  console.log('点击了右边的文字')
}

const agree = ref(false)
const isShowPass = ref(false)
const mobile = ref('13230000088')
const password = ref('abc12345')
const store = useUserStore()
const router = useRouter()
const route = useRoute()

const login = async () => {
  if (!agree.value) return showFailToast('请勾选用户协议')
  try {
    const res = isPass.value
      ? await loginByPassword(mobile.value, password.value)
      : await loginByMobile(mobile.value, code.value)
    store.setUser(res.data)
    router.push((route.query.returnUrl as string) || '/user')
    showSuccessToast('登录成功')
  } catch (error) {}
}

// const isPass = ref(true) //true密码登录 | false短信登录
// const code = ref('') //验证码
// const time = ref(0)
// const form = ref<FormInstance>() //获取表单实例
// let timeId: number //倒计时定时器ID
// const send = async () => {
//   if (time.value > 0) return
//   try {
//     //输入手机号时,对手机号进行校验
//     await form.value?.validate('mobile')
//     const { data } = await sendMobileCode(mobile.value, 'login')
//     showSuccessToast('发送成功')
//     //填入验证码
//     code.value = data.code
//     //倒计时开始
//     time.value = 60
//     timeId = window.setInterval(() => {
//       time.value--
//       if (time.value <= 0) window.clearInterval(timeId)
//     }, 1000)
//   } catch (error) {
//     console.log(error)
//   }
// }

// onUnmounted(() => {
//   window.clearInterval(timeId)
// })

const { isPass, code, time, form, send } = useSendCode(mobile.value)
</script>
<style lang="scss" scoped>
.active {
  color: red;
}

.login {
  padding-top: 46px;
  &-head {
    padding: 30px 30px 50px;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  .van-form {
    padding: 0 14px;
    .cp-cell {
      height: 52px;
      padding: 14px 16px;
      box-sizing: border-box;
      display: flex;
      align-items: center;
      .van-checkbox {
        a {
          color: var(--cp-primary);
          padding: 0 5px;
        }
      }
    }
  }
}
</style>
